package com.info.zhiduoduo.common.exception;

import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.common.utils.StringUtil;

import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;

/**
 * 验证各种判断条件,不满足则抛出指定状态的异常{@link SmartException}, {@link StatusMsg}
 * <p> 抛出的异常会被统一异常handler处理: {@code GlobalExceptionHandler}
 *
 * @author bob
 * @time 2020/9/2 10:37 下午
 */
public class Asserts {

	/**
	 * 验证object 不能为null
	 */
	public static void notNull(Object object) {
		notNull(object, StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证object 不能为null
	 */
	public static void notNull(Object object, StatusMsg statusMsg) {
		if (object == null) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证object 不能为null, 为null,则抛出{@link RollbackException}
	 */
	public static void notNullElseRollback(Object object, String msg) {
		if (object == null) throw new RollbackException(msg);
	}

	/**
	 * 验证object 必须为null, 否则抛异常
	 */
	public static void isNull(Object object) {
		isNull(object, StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证object 必须为null, 否则抛异常
	 */
	public static void isNull(Object object, StatusMsg statusMsg) {
		if (object != null) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证object 必须为null, 否则抛回滚异常
	 */
	public static void isNullElseRollback(Object object, String msg) {
		if (object == null) throw new RollbackException(msg);
	}

	/**
	 * 验证字符串不能为空
	 */
	public static void notEmpty(String text) {
		notEmpty(text, StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证字符串不能为空
	 */
	public static void notEmpty(String text, StatusMsg statusMsg) {
		if (StringUtil.isEmpty(text)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证字符串任何一个都不能为空,否则抛异常
	 */
	public static void notAnyEmpty(String... text) {
		notAnyEmpty(StatusMsg.StatusMsg_101, text);
	}

	/**
	 * 验证字符串任何一个都不能为空,否则抛异常
	 */
	public static void notAnyEmpty(StatusMsg statusMsg, String... text) {
		if (StringUtil.isAnyEmpty(text)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证集合不能为空
	 */
	public static <T> void notEmpty(Collection<T> collection) {
		notEmpty(collection, StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证集合不能为空
	 */
	public static <T> void notEmpty(Collection<T> collection, StatusMsg statusMsg) {
		if (CollectionUtils.isEmpty(collection)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证集合不能为空
	 */
	public static <T> void notEmptyElseRollback(Collection<T> collection, String statusMsg) {
		if (CollectionUtils.isEmpty(collection)) throw new RollbackException(statusMsg);
	}

	/**
	 * 验证字符串必须为空, 否则报错
	 */
	public static void isEmpty(String text) {
		isEmpty(text, StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证字符串必须为空, 否则报错
	 */
	public static void isEmpty(String text, StatusMsg statusMsg) {
		if (!StringUtil.isEmpty(text)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证字符串必须为空, 否则报错
	 */
	public static void isBlank(String text, StatusMsg statusMsg) {
		if (!StringUtil.isEmpty(text)) throw SmartException.of(statusMsg, text);
	}

	/**
	 * 验证必须为0,否则抛异常
	 */
	public static void equalZero(int num, StatusMsg statusMsg) {
		if (num > 0) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证必须 o1 == o2, 否则抛异常
	 */
	public static void equals(Object o1, Object o2) {
		equals(o1, o2, StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证必须 o1 == o2, 否则抛异常
	 */
	public static void equals(Object o1, Object o2, StatusMsg statusMsg) {
		if (o1 != o2) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证必须 o1 == o2, 否则抛异常
	 */
	public static void equals(String o1, String o2, StatusMsg statusMsg) {
		if (!StringUtil.equals(o1, o2)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证必须 o1 == o2, 否则抛异常
	 */
	public static void equals(Integer o1, Integer o2, StatusMsg statusMsg) {
		if (!NumberUtil.equals(o1, o2)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证必须 o1 != o2, 否则抛异常
	 */
	public static void notEquals(Object o1, Object o2) {
		notEquals(o1, o2, StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证必须 o1 != o2, 否则抛异常
	 */
	public static void notEquals(Object o1, Object o2, StatusMsg statusMsg) {
		if (o1 == o2) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证必须 o1 != o2, 否则抛异常
	 */
	public static void notEquals(Integer o1, Integer o2, StatusMsg statusMsg) {
		if (NumberUtil.equals(o1, o2)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证必须 o1 != o2, 否则抛异常
	 */
	public static void notEquals(String o1, String o2, StatusMsg statusMsg) {
		if (StringUtil.equals(o1, o2)) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证表达式必须为true,如果为false,则抛异常
	 */
	public static void isTrue(boolean expression) {
		if (!expression) throw SmartException.of(StatusMsg.StatusMsg_101);
	}

	/**
	 * 验证表达式必须为true,如果为false,则抛异常
	 */
	public static void isTrue(boolean expression, StatusMsg statusMsg) {
		if (!expression) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证表达式必须为true,如果为false,则抛异常
	 */
	public static void isTrue(boolean expression, String statusMsg) {
		if (!expression) throw SmartException.of(statusMsg);
	}

	/**
	 * 验证表达式必须为true,如果为false,则抛异常{@link RollbackException}
	 */
	public static void isTrueElseRollback(boolean expression, String msg) {
		if (!expression) throw new RollbackException(msg);
	}

	/**
	 * 验证表达式必须为false, 如果为true, 则抛异常.
	 *
	 * @param expression
	 * @param statusMsg
	 */
	public static void notTrue(boolean expression, StatusMsg statusMsg) {
		if (expression) throw SmartException.of(statusMsg);
	}
}
